package com.shop.controller;

import com.shop.pojo.DtsTopic;
import com.shop.service.DtsTopicService;
import com.shop.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName AdminTopicController
 * @Author xiao宋
 * @Date 19:53  2022/10/19
 * @Version 1.0
 * @Description TODO
 */
@Slf4j
@RestController
@RequestMapping("/admin/topic")
public class AdminTopicController {
    @Autowired
    DtsTopicService topicService;

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsTopic topic) {
        boolean flag = topicService.delete(topic);
        if (flag) {
            return ResponseUtil.ok();
        }
        {
            return ResponseUtil.fail(-1, "专题删除失败");
        }
    }

    /**
     * 新增商品专题
     *
     * @param topic
     * @return
     */
    @PostMapping("/create")
    public Object create(@RequestBody DtsTopic topic) {
        DtsTopic data;
        try {
            data = topicService.creat(topic);
        } catch (Exception e) {
            return ResponseUtil.fail(-1, e.getMessage());
        }
        return ResponseUtil.ok(data);
    }

    @PostMapping("/update")
    public Object update(@RequestBody DtsTopic topic) {
        DtsTopic data;
        try {
            data = topicService.update(topic);
        } catch (Exception e) {
            return ResponseUtil.fail(-1, e.getMessage());
        }
        return ResponseUtil.ok(data);
    }

    /**
     * 显示专题列表
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param title    标题
     * @param subtitle 子标题
     * @return
     */
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
            @RequestParam(value = "sort", required = false, defaultValue = "add_time") String sort,
            @RequestParam(value = "order", required = false, defaultValue = "desc") String order,
            String title, String subtitle
    ) {
        Map<String, Object> data = topicService.findByPage(page, limit, sort, order, title, subtitle);
        return ResponseUtil.ok(data);
    }

}
